<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * manage user
 *
 * @package     manage
 * @subpackage  
 * @category    user
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class User extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	  	$this->load->library('form_validation'); // 表单验证类
		$this->load->model('role_model'); // 加载角色模型
		$this->load->model('user_model'); // 加载用户模型 
		$this->load->library('my_md5');
		check_login(); // 检查是否登录
	}

	/**
	 * 
	 * @return [type] [list]
	 */
	public function index()
	{
		$total = $this->user_model->get_num_rows();
		if($total != 0){
			// 加载分页类
			$arr = array('page_total_num' => $total,'every_page_num'=>10,'page_url_name'=>'page_id');
			$this->load->library('page',$arr);
			$data['pager']	  = $this->page->pages_all();
			$data['user_arr'] = $this->user_model->get_all_data_and_page($this->page->limit);
		}else{
			$data['error'] = '暂无用户!';
		}
		$this->load->view('admin/user/list',$data);
	}

	/**
	 * 增加用户
	 */
	public function insert()
	{
		$data['role'] = $this->role_model->get_role_name(); // 角色
		if ($this->form_validation->run('user') == FALSE){
		    $this->load->view('admin/user/add',$data);
		}else{
			if(!empty($_POST['user_name'])){
				$arr = $this->input->post();
				$_insert['user_name'] 		= $arr['user_name'];
				// 将密码进行md5加密
				$_insert['user_password'] 	= $this->my_md5->to_md5($arr['user_password']);
				$_insert['user_nickname']	= $arr['user_nickname'];
				$_insert['user_role_id']	= $arr['user_role_id'];
				if($this->user_model->insert($_insert)){
					$this->sys_message->skip('增加用户成功',site_url('manage/user'));
				}else{
					if(user_model::$error == 'error'){
						$this->sys_message->show_error('该用户名已存在,请重新添加');
					}else{
						$this->sys_message->show_error('增加用户失败,请重新添加！');
					}
				}
			
			}
		}
	}

	/**
	 * 编辑用户信息
	 */
	public function edit()
	{
		// 默认值
		$data['user'] = $this->user_model->get_user_by_userid($this->input->get('u_id'));
		// 角色
		$data['role'] = $this->role_model->get_role_name();
		if ($this->form_validation->run('user') == FALSE){
		    $this->load->view('admin/user/edit',$data);
		}else{
			if(!empty($_POST['user_name'])){
				$arr = $this->input->post();
				$_update['user_name'] 		= $arr['user_name'];
				// 判断密码是否存在
				if($arr['user_password'] != ''){
					// 将密码进行md5加密
					$_update['user_password'] 	= $this->my_md5->to_md5($arr['user_password']);
				}
				$_update['user_nickname']	= $arr['user_nickname'];
				$_update['user_role_id']	= $arr['user_role_id'];
				if($this->user_model->update($_update,$this->input->get('u_id'))){
					$this->sys_message->skip('编辑用户成功',site_url('manage/user'));
					return ;
				}else{
					if(user_model::$error == 'error'){
						$this->sys_message->show_error('该用户名已存在,请重新编辑！');
						return ;
					}else{
						$this->sys_message->show_error('编辑用户失败,请重新编辑！');
						return ;
					}
				}
			}
		}
	}

	/**
	 * 删除用户
	 */
	public function delete()
	{
		if(!empty($_GET['uid']) && intval($_GET['uid'])){
			if($this->user_model->delete($_GET['uid'])){
				die('ok');
			}else{
				die('no');
			}
		}else{
			die('error_2');
		}
	}

	/**
	 * 更改密码
	 */
	public function change_password()
	{
		if(!empty($_POST['pass_old'])){
			// 判断是否和原始密码相同
			$u_id 	   = $this->session->userdata('system_user_id');
			$user_info = $this->user_model->get_user_by_userid($u_id);
			if($this->my_md5->md5_2($_POST['pass_old']) != $this->my_md5->get_md5($user_info['user_password'])){
				$this->sys_message->show_error('对不起！原始密码输入不正确，请重新输入!');
				return ;
			}
			// 更新密码
			$_update['user_password'] = $this->my_md5->to_md5($_POST['pass_new']);
			if($this->user_model->update($_update,$u_id)){
				$this->sys_message->skip('密码修改成功!',site_url('manage/user/change_password'));
				return ;
			}else{
				die('更新失败!');
			}
		}
		// 用户名
		$data['user_name'] = $this->session->userdata('system_user_name');
		$this->load->view('admin/user/change_password',$data);
	}
}